package com.ms.game.itembox.container;

import java.math.BigDecimal;
import java.util.ArrayList;

import com.ms.game.common.GameType;
import com.ms.game.itembox.GameCardType;
import com.ms.game.itembox.Item;
import com.ms.game.itembox.ItemSimpleData;
import com.ms.player.Player;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.common.Helper;
import com.xpec.c4.service.module.common.ItemInfoManager;

public class GameCardChecker {
	/**
	 * 玩家使用的道具
	 */
	private Item item;

	/**
	 * 要送去DC的道具資料
	 */
	private ArrayList<ItemSimpleData> useItemsInGame;
	
	/**
	 * 押注倍數
	 */
	private int playerBetCount;
	
	/**
	 * 卡片押注數
	 */
	private int cardBetCount;
	
	/**
	 * 是否可以使用
	 */
	private boolean canUse;
	
	/** 此遊戲卡進入Bonus類型 */
	private GameCardType gameCardType;
	
	/**
	 * 錯誤碼
	 */
	private int errorCode;
	
	public Item getItem() {
		return item;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public ArrayList<ItemSimpleData> getUseItemsInGame() {
		return useItemsInGame;
	}

	public void setUseItemsInGame(ArrayList<ItemSimpleData> useItemsInGame) {
		this.useItemsInGame = useItemsInGame;
	}

	public boolean isCanUse() {
		return canUse;
	}

	public void setCanUse(boolean canUse) {
		this.canUse = canUse;
	}

	public int getPlayerBetCount() {
		return playerBetCount;
	}
	
	public int getCardBetCount() {
		return cardBetCount;
	}

	public GameCardType getGameCardType() {
		return gameCardType;
	}

	public void setGameCardType(GameCardType gameCardType) {
		this.gameCardType = gameCardType;
	}
	
	/**
	 * 
	 * @param itemSn
	 * @param player
	 * @param gameType
	 */
	public void checkPersistentGameCard(String itemSn, Player player, GameType gameType) {
		//檢查使用卡片資訊
		if(player.isUseTrialCoin()){
			//試玩幣不能用道具卡
			this.errorCode = ErrorCode.TRAIL_PLAYER_CAN_NOT_USE_ITEM;
			return;
		}
		
		Item item = player.getItemBox().getItem(itemSn);
		if(item == null){
			this.errorCode = ErrorCode.ITEM_NOT_FOUND_FROM_PLAYER;
			return;
		}
		
		ItemBasic itemBasic = ItemInfoManager.getInstance().getItem(item.getItemId());
		if(itemBasic == null){
			this.errorCode = ErrorCode.ITEM_NOT_FOUND_FROM_BIN;
			return;
		}

		if(!itemBasic.isLevelEnoughToUse(player.getLevel())) { // 等級不足
			this.errorCode = ErrorCode.PLAYER_LEVEL_NOT_ENOUGH;
			return;
		}
		
		if (!itemBasic.isGameCard()) { // 不是機台卡
			this.errorCode = ErrorCode.ITEM_NOT_GAME_CARD;
			return;
		}

		if (item.getRefineLevel() <= 0) { // 沒精練的卡片不能用
			this.errorCode = ErrorCode.ITEM_NEED_REFINE;
			return;
		}

		ItemGameCard itemGameCard = (ItemGameCard) itemBasic;
		GameType useGameType = itemGameCard.getUseGame();
		ArrayList<String> notUseGames = itemGameCard.getNotUseGames();
		
		if (useGameType != null && useGameType != gameType) { //不是設定的遊戲，就不能使用
			this.errorCode = ErrorCode.ITEM_NOT_USE_IN_THIS_GAME;
			return;
		}
		
		if (notUseGames != null && notUseGames.contains(gameType.name())) { //在不能用的列表中，不能用
			this.errorCode = ErrorCode.ITEM_NOT_USE_IN_THIS_GAME;
			return;
		}
		
		GameCardType gameCardType = itemGameCard.getGameCardType();
		if(!gameCardType.isPersistent()) {
			this.errorCode = ErrorCode.INVALID_PARAM;
			return;
		}
		
		// 玩家的押注
		this.useItemsInGame = null;
		this.item = item;
		this.gameCardType = itemGameCard.getGameCardType();
		this.errorCode = ErrorCode.SUCCESS;
		this.canUse = true;
		
	}
	
	/**
	 * 一般機台卡檢查
	 * @param itemSn
	 * @param playerBetCount
	 * @param player
	 * @param gameType
	 */
	public void checkSingleUseGameCard(String itemSn ,Player player, GameType gameType, BigDecimal gainRate) {
		//檢查使用卡片資訊
		if(player.isUseTrialCoin()){
			//試玩幣不能用道具卡
			this.errorCode = ErrorCode.TRAIL_PLAYER_CAN_NOT_USE_ITEM;
			return;
		}
		
		Item item = player.getItemBox().getItem(itemSn);
		if(item == null){
			this.errorCode = ErrorCode.ITEM_NOT_FOUND_FROM_PLAYER;
			return;
		}
		
		ItemBasic itemBasic = ItemInfoManager.getInstance().getItem(item.getItemId());
		if(itemBasic == null){
			this.errorCode = ErrorCode.ITEM_NOT_FOUND_FROM_BIN;
			return;
		}
		
		if (!itemBasic.isGameCard()) {
			// 不是機台卡
			this.errorCode = ErrorCode.ITEM_NOT_GAME_CARD;
			return;
		}
		
		if(!itemBasic.isLevelEnoughToUse(player.getLevel())) { // 等級不足
			this.errorCode = ErrorCode.PLAYER_LEVEL_NOT_ENOUGH;
			return;
		}

		if (item.getRefineLevel() <= 0) { // 沒精練的卡片不能用
			this.errorCode = ErrorCode.ITEM_NEED_REFINE;
			return;
		}

		ItemGameCard itemGameCard = (ItemGameCard) itemBasic;
		GameType useGameType = itemGameCard.getUseGame();
		ArrayList<String> notUseGames = itemGameCard.getNotUseGames();
		
		if (useGameType != null && useGameType != gameType) { //不是設定的遊戲，就不能使用
			this.errorCode = ErrorCode.ITEM_NOT_USE_IN_THIS_GAME;
			return;
		}
		
		if (notUseGames != null && notUseGames.contains(gameType.name())) { //在不能用的列表中，不能用
			this.errorCode = ErrorCode.ITEM_NOT_USE_IN_THIS_GAME;
			return;
		}
		
		GameCardType gameCardType = itemGameCard.getGameCardType();
		if(gameCardType.isPersistent()) {
			this.errorCode = ErrorCode.INVALID_PARAM;
			return;
		}
		
		//比值不對
		if(!Helper.isEqual(itemGameCard.getGainRate(), gainRate)){
			this.errorCode = ErrorCode.ITEM_NOT_USE_IN_THIS_GAME;
			return;
		}
		
//		//算這張卡押注數
		int cardBetCount = itemGameCard.getRefineLevelBetCount().get(item.getRefineLevel());
//		int betCheck = checkBetCount(playerBetCount);
//		if(betCheck != ErrorCode.SUCCESS){
//			this.errorCode = betCheck;
//		}
		
//		if (playerBetCount != cardBetCount) {
//			// 玩家押注額要跟道具等級的押注額相同
//			this.errorCode = ErrorCode.ITEM_BET_COUNT_DIFFERENT_WITH_REFINE_LEVEVL;
//			return;
//		}

		// 組成去DC要扣的
		ArrayList<ItemSimpleData> simpleDatas = new ArrayList<ItemSimpleData>();
		ItemSimpleData simpleData = new ItemSimpleData();
		simpleData.setItemSn(item.getSn());
		simpleData.setCount(1);
		simpleData.setItemid(item.getItemId());
		simpleDatas.add(simpleData);

		this.cardBetCount = cardBetCount;
		this.useItemsInGame = simpleDatas;
		this.item = item;
		this.gameCardType = gameCardType;
		this.errorCode = ErrorCode.SUCCESS;
		this.canUse = true;

		return;
	}
	
	/**
	 * 押注額檢查
	 * @param playerBetCount
	 * @return ErrorCode
	 */	
	public int checkBetCount(int playerBetCount){
		if (gameCardType == GameCardType.ADVANCED_BET_KEY) { //如果是高級鑰匙
			if(playerBetCount <= 0 && playerBetCount > cardBetCount) {
				//到時候改
				return ErrorCode.ITEM_BET_COUNT_DIFFERENT_WITH_REFINE_LEVEVL;
			}
		} else {
			if (playerBetCount != cardBetCount) {
				// 玩家押注額要跟道具等級的押注額相同
				return ErrorCode.ITEM_BET_COUNT_DIFFERENT_WITH_REFINE_LEVEVL;
			}	
		}
		
		this.playerBetCount = playerBetCount;
		return ErrorCode.SUCCESS;
	}
	
	public boolean isNormalGameCard(){
		if (gameCardType == null) {
			return false;
		}

		return gameCardType.isNormalGameCard();
	}
	
	/**
	 * 是否是使用金鑰卡</br>
	 * @return
	 */
	public boolean isUseAdvanceKeyCard(){
		if(canUse && gameCardType != null  && gameCardType == GameCardType.ADVANCED_BET_KEY){	
			//可使用，並且type 是金鑰卡
			return true;
		}
		return false;
	}
	
	/**
	 * 回傳使用的機台卡的basic Item Id</br> 
	 * 是要傳給client看的 </br>
	 * @return
	 */
	public String getCardBasicItemId() {
		if(canUse && item != null){
			return item.getBasicItemId();
		}
		return null;
	}
}
